package com.qujia.station.utils;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;

public class XmlUtils {
    public static String getEventKey(String xml) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));

            Element root = document.getDocumentElement();
            NodeList eventKeyNodeList = root.getElementsByTagName("EventKey");
            if (eventKeyNodeList.getLength() > 0) {
                Element eventKeyElement = (Element) eventKeyNodeList.item(0);
                return eventKeyElement.getTextContent();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
